//
//  XYDiscoverSearchView.swift
//  01.XinLangWB
//
//  Created by xiaoyudong on 16/4/7.
//  Copyright © 2016年 苏小样. All rights reserved.
//

import UIKit

class XYDiscoverSearchView: UIView {
   
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var textView: UITextField!
    override func awakeFromNib() {
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: self.bounds.height)
        //设置搜索图标
        self.textView.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        self.textView.leftView?.frame = CGRect(x: 0, y: 0, width: self.bounds.height, height: self.bounds.height)
        //设置图片的显示模式
        self.textView.leftView?.contentMode = .Center
        //需要设置leftView为始终显示
        self.textView.leftViewMode = .Always
//        //设置圆角
//        textView.layer.cornerRadius = 8
//        textView.layer.masksToBounds = true
        
        
    }
    
    //类方法
    class func loadSearchView() -> XYDiscoverSearchView {
    let v = NSBundle.mainBundle().loadNibNamed("XYDiscoverSearchView", owner: nil, options: nil).last  as! XYDiscoverSearchView
        return v
    }
    ///自动布局的动画执行
    //1. 修改约束
    //2. 在动画闭包中强制刷新页面

    @IBAction func beginEdit(sender: AnyObject) {
        //先修改约束
        //系统 只是收集了约束的变化  会在layoutSubViews方法中去更新页面
        rightCons.constant = cancelBtn.frame.width
        //在动画闭包中 执行 强制刷新页面
        //        //系统默认的动画时间 0.25
        UIView.animateWithDuration(0.25) { () -> Void in
            //提前刷新页面
            // layoutIfNeeded是根据 自动布局 来修改frame
            self.layoutIfNeeded()
        }
    }
  
    @IBAction func cancelButtonDidClick(sender: AnyObject) {
        
        //取消键盘的第一响应者
        textView.resignFirstResponder()
        rightCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        
    }
}
